package com.oitsm.utils;

import java.io.FileInputStream;
import java.util.Properties;

/**
 * 提供 sysconfig.properties文件中配置的属性的访问
 * @author Arab Runner
 * Jun 25, 2011 11:57:18 AM
 */
public class PropUtil {
    public static final Properties SYS_PROP = new Properties();
    
    /**
     */
    public static void init(){
        try{
            String filePath = PropUtil.class.getClassLoader().getResource("sysconfig.properties").getPath();
            SYS_PROP.load(new FileInputStream(filePath));
        }catch(Throwable t){
            t.printStackTrace();
        }
    }
    /**
     * get
     * @param key
     * @param defaultValue
     * @return
     */
    public static String get(String key,String defaultValue){
        return SYS_PROP.getProperty(key, defaultValue);
    }
    /**
     * get
     * @param key
     * @return
     */
    public static String get(String key){
        return SYS_PROP.getProperty(key);
    }
    
    
    /**
     * set
     * @param key
     * @param value
     * @return
     */
    public static void set(String key,String value){
        SYS_PROP.put(key, value);
    }
    /**
     * @param key
     * @param defaultVal
     * @return
     */
    public static Integer getInteger(String key,Integer defaultVal){
    	try{
    		return getInteger(key);
    	}catch(Exception e){
    		return defaultVal;
    	}
    }
    /**
     * @param key
     * @return
     */
    public static Integer getInteger(String key){
    	return getIntegerByVal(get(key));
    }
    
    
    private static Integer getIntegerByVal(String val){
    	return Integer.valueOf(val);
    }
    
    /**
     * @param key
     * @param defaultVal
     * @return
     */
    public static Long getLong(String key,Long defaultVal){
    	try{
    		return getLong(key);
    	}catch(Exception e){
    		return defaultVal;
    	}
    }
    
    public static Long getLong(String key){
    	return getLongByVal(get(key));
    }
    
    
    private static Long getLongByVal(String val){
    	return Long.valueOf(val);
    }
    
    /**
     * @param key
     * @param defaultValue
     * @return
     */
    public static Boolean getBoolean(String key,String defaultValue){
    	return getBooleanByVal(get(key,defaultValue));
    }
    /**
     * @param key
     * @return
     */
    public static Boolean getBoolean(String key){
        return getBooleanByVal(get(key));
    }
    private static Boolean getBooleanByVal(String val){
    	return Boolean.valueOf(val);
    }
    /**
     * @return
     */
    public static String getClassesPath(){
		return PropUtil.class.getClassLoader().getResource("").getPath();
    }
    /**
     * @param name
     * @return
     */
    public static String getPath(String name){
    	return PropUtil.class.getClassLoader().getResource(name).getPath();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub 
    }

}
